var CreateItemForm;
var CreateItemWindow;

var ArticleField;
var CompanyNameField;
var DescriptionField;
var CompaniesDataStore;

Ext.onReady(function(){

	CompaniesDataStore = new Ext.data.Store({
		id: 'CompaniesDataStore',
		proxy: new Ext.data.HttpProxy({
                url: 'index.php?module=catalogue&action=viewcompanies&json=1', 
                method: 'POST'
        }),
        
		reader: new Ext.data.JsonReader({
			root: 'results',
			totalProperty: 'total',
			id: 'id'
		},[ 
			{name: 'id', type: 'int', mapping: 'id'},
			{name: 'name', type: 'string', mapping: 'name'},
			{name: 'description', type: 'string', mapping: 'description'}
		]),
		sortInfo:{field: 'id', direction: "ASC"}
    });

	ArticleField = new Ext.form.TextField({
		id: 'ArticleField',
		fieldLabel: 'Артикул',
		maxLength: 255,
		allowBlank: false,
		anchor : '95%',
		maskRe: /([a-zA-Z0-9\s]+)$/
	});
      
	DescriptionField = new Ext.form.TextField({
		id: 'DescriptionField',
		fieldLabel: 'Описание',
		allowBlank: true,
		anchor : '95%',    
		maskRe: /([a-zA-Z0-9\s]+)$/  
    });
	  
	CompanyNameField = new Ext.form.ComboBox({
		id:'CompanyNameField',
		fieldLabel: 'Party',
		store: CompaniesDataStore,
		mode: 'remote',
		displayField: 'name',
		allowBlank: false,
		valueField: 'id',
		anchor:'95%',
		triggerAction: 'all'
    });

	CreateItemForm = new Ext.FormPanel({
		labelAlign: 'top',
		bodyStyle:'padding:5px',
		width: 600,        
		items: [{
			layout:'column',
			border:false,
			items:[{
				columnWidth:0.5,
				layout: 'form',
				border:false,
				items: [ArticleField, DescriptionField]
			},{
				columnWidth:0.5,
				layout: 'form',
				border:false,
				items: [CompanyNameField]
			}]
		}],
		buttons: [{
			text: 'Save and Close',
			handler: createItem
			},{
			text: 'Cancel',
			handler: function(){
				CreateItemWindow.hide();
			}
		}]
	});
	  
	CreateItemWindow = new Ext.Window({
		id: 'CreateItemWindow',
		title: 'Новый товар',
		closable:true,
		width: 610,
		height: 250,
		plain:true,
		layout: 'fit',
		items: CreateItemForm
	});
	
	function createItem(){
		if(isCreateItemFormValid()){
			Ext.Ajax.request({   
				waitMsg: 'Please wait...',
				url: 'index.php?module=price&action=additem&json=1',
				params: {
					article:			ArticleField.getValue(),
					description:		DescriptionField.getValue(),
					companyID:		CompanyNameField.getValue()
				}, 
				success: function(response){              
					var result=eval(response.responseText);
					switch(result){
						case 1:
							Ext.MessageBox.alert('Creation OK','The president was created successfully.');
							FullPriceDataStore.reload();
							CreateItemWindow.hide();
						break;
						default:
							Ext.MessageBox.alert('Warning','Could not create the president.');
						break;
					}        
				},
				failure: function(response){
					var result=response.responseText;
					Ext.MessageBox.alert('error','could not connect to the database. retry later');         
				}
			});
		} 
		else{
			Ext.MessageBox.alert('Warning', 'Your Form is not valid!');
		}
	}
	
	function isCreateItemFormValid(){
		return(ArticleField.isValid());
	}
});
function resetCreateItemForm(){
	ArticleField.setValue('');
	DescriptionField.setValue('');
}